package queue;
import org.junit.Test;

import java.util.Scanner;

public class CircleQueue {
    public CircleQueue() {
    }
    int maxSize=3;
    String[] strings=new String[maxSize];
    int rear=-1;
    int font=-1;
    @Test
    public  void test() {
        while(true){
            System.out.println("----------------------");
            System.out.println("a(add)添加元素");
            System.out.println("d(del)删除元素");
            System.out.println("g(gethead)获取头部元素");
            System.out.println("s(show)展示库中数据");
            Scanner scanner = new Scanner(System.in);
            String key=scanner.next();
            switch(key){
                case "a":
                    System.out.println("请输入你要添加的字符串");
                    String yuansu=scanner.next();
                    add(yuansu);
                    break;
                case "d":del();
                    break;
                case "g":
                    String head = getHead();
                    System.out.println("获取到的头节点:"+head);
                    break;
                case "s":show();
                    break;
            }
        }
    }

    private void add(String s){
//        添加之前判断队列是否已经满员
        if(font==(rear+1)%maxSize)
            System.out.println("队列已满,无法添加元素");
        else{
            rear++;
            strings[rear%maxSize]=s;
        }
        System.out.println();
        System.out.println("font:"+font);
        System.out.println("rear"+rear);
    }
    private void del(){
//       删除之前确保队列不为空
        if(font%maxSize==rear%maxSize){
            System.out.println("队列已空");
        }else{
            strings[(font+1)%maxSize]="";
            font++;
        }
        System.out.println();
        System.out.println("font:"+font);
        System.out.println("rear"+rear);
    }
    private String getHead(){
//        获取之前判断队列是否为空
        if (font%maxSize==rear%maxSize){
            System.out.println("队列为空");
            System.out.println();
            System.out.println("font:"+font);
            System.out.println("rear"+rear);
            return "队列空,无元素";
        }
        else{
            System.out.println("");
            System.out.println("font:"+font);
            System.out.println("rear"+rear);
           return strings[(font+1)%maxSize];
        }

    }
    private void show(){
        System.out.println("库中数据展示-----------------");
        for (String s:strings) {
            System.out.println(s);
        }
        System.out.println("");
        System.out.println("font:"+font);
        System.out.println("rear"+rear);
    }
}
